﻿// <copyright file="ChamberConverter.cs" company="Microsoft Corporation">
// Copyright (c) 2008 All Right Reserved
// </copyright>
// <author>Michael S. Scherotter</author>
// <email>mischero@microsoft.com</email>
// <date>2009-03-03</date>
// <summary>A Value Converter</summary>

namespace NYTimes.ValueConverters
{
    using System;
    using System.Net;
    using System.Windows;
    using System.Windows.Controls;
    using System.Windows.Data;
    using System.Windows.Documents;
    using System.Windows.Ink;
    using System.Windows.Input;
    using System.Windows.Media;
    using System.Windows.Media.Animation;
    using System.Windows.Shapes;

    /// <summary>
    /// Value converter for radio buttons and NYTimes.Chamber values
    /// </summary>
    /// <example>
    /// Example from CongressPage.xaml sample
    /// <code>
    /// <![CDATA[
    /// <RadioButton Content="House" DataContext="{StaticResource MembersOfCongressDS}" 
    ///     IsChecked="{Binding Chamber, Mode=TwoWay, Converter={StaticResource ChamberConverter}, ConverterParameter=House}" />
    /// <RadioButton Content="Senate" DataContext="{StaticResource MembersOfCongressDS}" 
    ///     IsChecked="{Binding Chamber, Mode=TwoWay, Converter={StaticResource ChamberConverter}, ConverterParameter=Senate}" />
    /// ]]>
    /// </code>
    /// </example>
    public class ChamberConverter : IValueConverter
    {
        #region IValueConverter Members

        /// <summary>
        /// Modifies the source data before passing it to the target for display in the UI. 
        /// </summary>
        /// <param name="value">a NYTimes.Chamber enumerated value</param>
        /// <param name="targetType">The Type of data expected by the target dependency property.</param>
        /// <param name="parameter">"House" or "Senate"</param>
        /// <param name="culture">The culture of the conversion.</param>
        /// <returns>The value to be passed to the target dependency property. </returns>
        public object Convert(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
        {
            if (value == null)
            {
                throw new ArgumentNullException("value");
            }

            if (parameter == null)
            {
                throw new ArgumentNullException("parameter");
            }

            var chamberValue = value.ToString();

            var controlValue = parameter.ToString();

            return chamberValue == controlValue;
        }

        /// <summary>
        /// If the value is true, then return the parameter as a NYTimes.Chamber enumeration
        /// </summary>
        /// <param name="value">a boolean value indicating whether the value is being set</param>
        /// <param name="targetType">The Type of data expected by the source object.</param>
        /// <param name="parameter">"House" or "Senate"</param>
        /// <param name="culture">The culture of the conversion.</param>
        /// <returns>a NYTimes.Chamber enumerated value</returns>
        public object ConvertBack(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
        {
            bool set = (bool)value;

            if (set)
            {
                if (parameter == null)
                {
                    throw new ArgumentNullException("parameter");
                }

                string chamberName = parameter.ToString();

                switch (chamberName)
                {
                    case "House":
                        return Chamber.House;

                    case "Senate":
                        return Chamber.Senate;
                }
            }

            return DependencyProperty.UnsetValue;
        }

        #endregion
    }
}
